home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / QuickDraw / Rotate Bitmap 90° / Rotate.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  3.6 KB  |  141 lines  |  [TEXT/KAHL]

  1. #define    BIT00       0x01
  2. #define    BIT01       0x02
  3. #define    BIT02       0x04
  4. #define    BIT03       0x08
  5. #define    BIT04       0x10
  6. #define    BIT05       0x20
  7. #define    BIT06       0x40
  8. #define    BIT07       0x80
  9.  
  10. BitMap        *macBitMap;
  11.  
  12. rotate()
  13. {
  14.     Ptr                srcPtr, destPtr, destColPtr, myBaseAddr;
  15.     BitMap            *newBitmap;
  16.     short            destRowBytes, bitchar, srcByte, srcH, srcV, srccols, srcrows, i;
  17.     Rect            destRect;
  18.  
  19.     /* rotate boundary rectangle */
  20.     srcH = macBitMap->bounds.right - macBitMap->bounds.left;
  21.     srcV = macBitMap->bounds.bottom - macBitMap->bounds.top;
  22.     SetRect(&destRect, macBitMap->bounds.left, macBitMap->bounds.top,
  23.             macBitMap->bounds.left+srcV, macBitMap->bounds.top+srcH);
  24.  
  25.     /* allocate destination buffer */
  26.     destRowBytes  = (srcV+7) >> 3;
  27.     newBitmap = (BitMap *) NewPtrClear(sizeof(BitMap));
  28.     myBaseAddr = (Ptr) NewPtrClear(destRowBytes*srcH);
  29.  
  30.     /* set-up src to rotated destination scan */
  31.     srccols = macBitMap->rowBytes;
  32.     srcrows = srcV;
  33.     srcPtr = macBitMap->baseAddr;
  34.     bitchar = BIT00;
  35.     destColPtr= myBaseAddr + destRowBytes - 1;
  36.     destPtr   = destColPtr;
  37.  
  38.     /* scan src row and rotate into dest col */
  39.     while (srcrows-- > 0) {
  40.         for (i=0; i<srccols; i++) {
  41.             srcByte = *(srcPtr++);
  42.             if (srcByte & BIT07) *destPtr |= bitchar;
  43.             destPtr += destRowBytes;
  44.             if (srcByte & BIT06) *destPtr |= bitchar;
  45.             destPtr += destRowBytes;
  46.             if (srcByte & BIT05) *destPtr |= bitchar;
  47.             destPtr += destRowBytes;
  48.             if (srcByte & BIT04) *destPtr |= bitchar;
  49.             destPtr += destRowBytes;
  50.             if (srcByte & BIT03) *destPtr |= bitchar;
  51.             destPtr += destRowBytes;
  52.             if (srcByte & BIT02) *destPtr |= bitchar;
  53.             destPtr += destRowBytes;
  54.             if (srcByte & BIT01) *destPtr |= bitchar;
  55.             destPtr += destRowBytes;
  56.             if (srcByte & BIT00) *destPtr |= bitchar;
  57.             destPtr += destRowBytes;
  58.         }
  59.         if (bitchar==BIT07) {
  60.             bitchar = BIT00;
  61.             destColPtr--;
  62.         } else {
  63.             bitchar <<= 1;
  64.         }
  65.  
  66.         destPtr = destColPtr;
  67.     }
  68.     
  69.     /* remove src bitmap (portbits for offscreen port) */
  70.     DisposPtr((Ptr)macBitMap->baseAddr);
  71.     DisposPtr((Ptr)macBitMap);
  72.     
  73.     /* store new src in object */
  74.     macBitMap   = (BitMap *)newBitmap;
  75.     macBitMap->baseAddr = (Ptr) myBaseAddr;
  76.     macBitMap->rowBytes = destRowBytes;
  77.     macBitMap->bounds   = destRect;
  78. }
  79.  
  80. main()
  81. {
  82.     WindowPtr        mainWinPtr;
  83.     OSErr            error;
  84.     SysEnvRec        theWorld;
  85.     Rect            windRect, ovalRect, myBounds;
  86.     short            myRowBytes;
  87.     
  88.     /* Make sure ColorQD exists. */
  89.     error = SysEnvirons(1, &theWorld);
  90.     if (theWorld.hasColorQD == false) {
  91.         SysBeep(50);
  92.         ExitToShell();
  93.     }
  94.     
  95.     /* Initialize all the needed managers. */
  96.     InitGraf(&qd.thePort);
  97.     InitFonts();
  98.     InitWindows();
  99.     InitMenus();
  100.     TEInit();
  101.     InitDialogs(nil);
  102.     InitCursor();
  103.  
  104.     /* Define output window with an inset clip region. */
  105.     SetRect(&windRect, 100, 100, 404, 404);
  106.     mainWinPtr = NewWindow(nil, &windRect, "\pJohn", true, documentProc, (WindowPtr) -1, false, 0);
  107.     SetPort(mainWinPtr);
  108.  
  109.     SetRect(&windRect, 0, 0, 304, 304);
  110.     EraseRect(&windRect);
  111.     SetRect(&ovalRect, 15, 15, 60, 120);
  112.     PaintOval(&ovalRect);
  113.     MoveTo(10,140);
  114.     DrawString("\PHi there.");
  115.     MoveTo(0,0);
  116.     LineTo(303,303);
  117.     MoveTo(0,303);
  118.     LineTo(303,0);
  119.  
  120.     /* rotate boundary rectangle */
  121.     myBounds = windRect;
  122.     myRowBytes = (myBounds.right - myBounds.left + 7) >> 3;
  123.     macBitMap = (BitMap *) NewPtrClear(sizeof(BitMap));
  124.     macBitMap->baseAddr = (Ptr) NewPtrClear(myRowBytes * (myBounds.bottom - myBounds.top));
  125.     macBitMap->bounds = myBounds;
  126.     macBitMap->rowBytes = myRowBytes;
  127.  
  128.     CopyBits(&(*mainWinPtr).portBits, macBitMap, &windRect, &macBitMap->bounds, 0, nil);
  129.  
  130.     /* Wait until user clicks button. */
  131.     do {
  132.     } while (!Button());
  133.  
  134.     /* Wait until user clicks button. */
  135.     do {
  136.     rotate();
  137.     CopyBits(macBitMap, &(*mainWinPtr).portBits, &macBitMap->bounds, &windRect, 0, nil);
  138.     } while (!Button());
  139. }
  140.  
  141.